// 32 bits files management

#include <windows.h>
#include <extend.h>

#define FS_END    2

//----------------------------------------------------------------------------//

HANDLE _fopen( LPSTR szName, DWORD wMode )
{
   DWORD dwMode = 0, dwShare = 0;

   if( ! wMode )
      dwMode = GENERIC_READ;

   if( wMode & FO_WRITE )
      dwMode = GENERIC_WRITE;

   if( wMode & FO_READWRITE )
      dwMode = GENERIC_READ | GENERIC_WRITE;

   if( wMode & FO_SHARED )
      dwShare = FILE_SHARE_READ | FILE_SHARE_WRITE;

   return CreateFile( szName, dwMode, dwShare, 0, OPEN_EXISTING,
                      FILE_ATTRIBUTE_ARCHIVE, 0 );
}

//----------------------------------------------------------------------------//

HANDLE _fcreate( LPSTR szName, DWORD wMode )
{
   DWORD dwMode = 0, dwShare = 0;

   if( ! wMode )
      dwMode = GENERIC_READ;

   if( wMode & FO_WRITE )
      dwMode = GENERIC_WRITE;

   if( wMode & FO_READWRITE )
      dwMode = GENERIC_READ | GENERIC_WRITE;

   if( wMode & FO_SHARED )
      dwShare = FILE_SHARE_READ | FILE_SHARE_WRITE;

   return CreateFile( szName, dwMode, dwShare, 0, CREATE_NEW,
                      FILE_ATTRIBUTE_ARCHIVE, 0 );
}

//----------------------------------------------------------------------------//

LONG _fread( HANDLE hFile, LPVOID pBuffer, DWORD lBytes )
{
   DWORD dwRead;

   ReadFile( hFile, pBuffer, lBytes, &dwRead, 0 );

   return dwRead;
}

//----------------------------------------------------------------------------//

LONG _fwrite( HANDLE hFile, LPVOID pBuffer, DWORD lBytes )
{
   DWORD dwWritten;

   WriteFile( hFile, pBuffer, lBytes, &dwWritten, 0 );

   return dwWritten;
}

//----------------------------------------------------------------------------//

BOOL _fclose( HANDLE hFile )
{
   return CloseHandle( hFile );
}

//----------------------------------------------------------------------------//

DWORD _fseek( HANDLE hFile, LONG lOffset, DWORD dwOrigin )
{
   return SetFilePointer( hFile, lOffset, 0, dwOrigin );
}

//----------------------------------------------------------------------------//

BOOL _ferase( LPSTR szFileName )
{
   return DeleteFile( szFileName );
}

//----------------------------------------------------------------------------//

BOOL _fcommit( HANDLE hFile )
{
   return FlushFileBuffers( hFile );
}

//----------------------------------------------------------------------------//

CLIPPER FCREATE()
{
   _retnl( ( LONG ) _fcreate( _parc( 1 ), _parni( 2 ) ) );
}

//----------------------------------------------------------------------------//

CLIPPER FOPEN()
{
   _retnl( ( LONG ) _fopen( _parc( 1 ), _parni( 2 ) ) );
}

//----------------------------------------------------------------------------//

CLIPPER FREAD()
{
   _retnl( _fread( ( HANDLE ) _parnl( 1 ), ( LPVOID ) _parnl( 2 ),
           ( DWORD ) _parnl( 3 ) ) );
}

//----------------------------------------------------------------------------//

CLIPPER FWRITE()
{
   _retnl( _fwrite( ( HANDLE ) _parnl( 1 ), _parc( 2 ),
           ( DWORD ) _parnl( 3 ) ) );
}

//----------------------------------------------------------------------------//

CLIPPER FCLOSE()
{
   _retl( _fclose( ( HANDLE ) _parnl( 1 ) ) );
}

//----------------------------------------------------------------------------//

CLIPPER FSEEK()
{
   _retnl( _fseek( ( HANDLE ) _parnl( 1 ), _parnl( 2 ), _parnl( 3 ) ) );
}

//----------------------------------------------------------------------------//

CLIPPER FERASE()
{
   _retl( _ferase( _parc( 1 ) ) );
}

//----------------------------------------------------------------------------//

CLIPPER FCOMMIT()
{
   _retl( _fcommit( ( HANDLE ) _parnl( 1 ) ) );
}

//----------------------------------------------------------------------------//

void LogFile( LPSTR szFileName, LPSTR szText )
{
   HANDLE hFile;
   LPSTR crlf = "\n";

   if( ( hFile = _fopen( szFileName, FO_WRITE ) ) != -1 ||
       ( hFile = _fcreate( szFileName, FO_WRITE ) ) != -1 )
   {
      _fseek( hFile, 0, FS_END );
      _fwrite( hFile, szText, _strlen( szText ) );
      _fwrite( hFile, crlf, _strlen( crlf ) );
      _fclose( hFile );
   }
}

//----------------------------------------------------------------------------//
